package org.tvolkov.mycompany

class EmployeeService {
	
	static final def NAME = "name";
	static final def HIRED = "hired";
	static final def SKILLS = "skills";
	static final def PHONE = "phone";
	static final def EMAIL = "email";
	static final def PROJECT = "project";
	static final def PARENT_NODE = "parentNode";
	static final def PARENT = "parentProject";
	
	def getEmployeeList(params){
		def name = Project.get(params["id"]);
		return Employee.findAllByProject(name).collect{[
						text : it.name,
						id   : ("employee" + it.id),
						leaf: true
					]};
	}
	
	def addNewEmployee(params){
		println("EmployeeService: addNewEmployee: params: " + params);
		def parentProject = params["parentProject"];
		if (parentProject){
			def empl = new Employee();
			if (params[NAME]){
				empl.name = params[NAME];
				println("EmployeeService: addNewEmployee: set name " + empl.name);
			}
			if(params[HIRED]){
				empl.hireDate = Date.parse("MM/dd/yyyy", params[HIRED]);
				println("EmployeeService: addNewEmployee: set hired " + empl.hireDate);
			}
			if (params[SKILLS]){
				empl.skills = params[SKILLS];
				println("EmployeeService: addNewEmployee: set skills " + empl.skills);
			}
			if (params[PHONE]){
				empl.phone = params[PHONE];
				println("EmployeeService: addNewEmployee: set phone " + empl.phone);
			}
			if (params[EMAIL]){
				empl.email = params[EMAIL];
				println("EmployeeService: addNewEmployee: set email " + empl.email);
			}
			if (params[PARENT]){
				empl.project = Project.get(params[PARENT]);
				println("EmployeeService: addNewEmployee: set project " + empl.project.name);
			}
			if (empl.save() != null){
				println("EmployeeService: add: success");
				return ["success": "true"];
			} else {
				println("EmployeeService: add: fail1");
				return ["success": "false"];
			}
		} else {
			println("EmployeeService: add: fail2");
			return ["success": "false"];
		}
	}
	
	def removeEmployee(params){
		println("EmployeeController: remove: " + params);
		Employee.get(params["nodeid"]).delete();
		return {["success": "true"]};
	}
		
	def getEmployeeDetails(params){
		def empl = Employee.get(params["nodeid"]);
		return [text: empl.name,
				id: ("employee" + empl.id),
				hired: empl.hireDate,
				skills: empl.skills,
				phone: empl.phone,
				email: empl.email,
				project: empl.project.name];		
	}
	
	def editEmployee(params){
		println("EmployeeService: editEmployee: params: " + params);
		
		def empl = Employee.get(params["nodeid"]);
		
		if (params[NAME]){
			if (empl.name != params[NAME]){
				empl.name = params[NAME];
				println("EmployeeService: editEmployee: set name " + params[NAME]);
			} else {
				println("EmployeeService: editEmployee: set namd fail");
			}
			
			if (params[SKILLS]){
				if (empl.skills != params[SKILLS]){
					empl.skills = params[SKILLS];
					println("EmployeeService: editEmployee: set skills " + params[SKILLS]);
				} else {
					println("EmployeeService: editEmployee: set skills fail");
				}
			}
			
			if (params[PHONE]){
				if (empl.phone != params[PHONE]){
					empl.phone = params[PHONE];
					println("EmployeeService: editEmployee: set phone: " + params[PHONE]);
				} else {
					println("EmployeeService: editEmployee: set phone fail");
				}
			}
			
			if (params[EMAIL]){
				if (empl.email != params[EMAIL]){
					empl.email = params[EMAIL];
					println("EmployeeService: editEmployee: set email " + params[EMAIL]);
				} else {
					println("EmployeeService: editEmployee: set email fail");
				}
			}
			
			if (params[PROJECT]){
				if (empl.project.name != params[PROJECT]){
					empl.project = Project.findByName(params[PROJECT]);
					println("EmployeeService: editEmployee: set project " + params[PROJECT]);
				} else {
					println("EmployeeService: editEmployee: set project fail");
				}
			}
			
			if (empl.save()){
				return ["success": "true"];
			} else {
				return ["success": "false"];
			}
		}
	}
}
